package com.wjk.kylin.modules.system.notice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjk.kylin.core.common.aspect.Log;
import com.wjk.kylin.core.common.controller.BaseController;
import com.wjk.kylin.core.common.data.RequestData;
import com.wjk.kylin.core.common.data.ResponseData;
import static com.wjk.kylin.core.constant.CommonConstant.BusinessType.*;

import com.wjk.kylin.core.common.valid.insert;
import com.wjk.kylin.core.common.valid.update;
import com.wjk.kylin.core.utils.PageUtils;
import com.wjk.kylin.modules.system.notice.entity.Notice;
import com.wjk.kylin.modules.system.notice.service.INoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  通知 前端控制器
 * </p>
 *
 * @author wangjkui
 * @since 2020-06-01
 */
@Api("通知")
@RestController
@RequestMapping("/sys/notice")
public class NoticeController extends BaseController {

    @Autowired
    private INoticeService noticeService;

    @PreAuthorize("hasAuthority('system:notice:list')")
    @ApiOperation("列表")
    @Log(title = "通知-列表",businessType = LIST,isSaveResponseData = false)
    @GetMapping("/list")
    public ResponseData list(Notice notice) {
        IPage<Notice> page = noticeService.findPage(new RequestData<Notice>(), notice);
        return ResponseData.success().data("page", new PageUtils(page));
    }

    @PreAuthorize("hasAuthority('system:notice:query')")
    @ApiOperation("详情")
    @Log(title = "通知-详情",businessType = GET)
    @GetMapping("/{noticeId}")
    public ResponseData get(@PathVariable String noticeId) {
        Notice notice = noticeService.getById(noticeId);
        return ResponseData.success().data("notice", notice);
    }

    @PreAuthorize("hasAuthority('system:notice:add')")
    @ApiOperation("新增")
    @Log(title = "通知-新增",businessType = INSERT)
    @PostMapping
    public ResponseData save(@Validated(insert.class) @RequestBody Notice notice) {
        boolean flag = noticeService.save(notice);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("新增失败");
        }
    }

    @PreAuthorize("hasAuthority('system:notice:edit')")
    @ApiOperation("编辑")
    @Log(title = "通知-编辑",businessType = UPDATE)
    @PutMapping
    public ResponseData update(@Validated(update.class) @RequestBody Notice notice) {
        boolean flag = noticeService.update(notice);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("编辑失败");
        }
    }

    @PreAuthorize("hasAuthority('system:notice:delete')")
    @ApiOperation("删除")
    @Log(title = "通知-删除",businessType = DELETE)
    @DeleteMapping("/{noticeIds}")
    public ResponseData delete(@PathVariable String[] noticeIds) {

        boolean flag = noticeService.deleteByIds(noticeIds);
        if (flag) {
            return ResponseData.success();
        } else {
            return ResponseData.error("删除失败");
        }
    }


}
